#include "streams.h"
#include "string.h"


/////////////////////////////////////////////////////////////
// class CTextFileCharStream


int CTextFileCharStream::GetCh()
{ 
    return fgetc(m_File);
}


void CTextFileCharStream::PushBack(int ch)
{ 
    ungetc(ch, m_File);
}

    
//////////////////////////////////////////////////////////////
// class CMemCharStream

CMemCharStream::CMemCharStream(uint8_t *stream, size_t size)
{
    m_pStream = stream;
    m_Size = size;
    m_Position = 0;
}


CMemCharStream::CMemCharStream(char *text)
{
    m_pStream = (uint8_t*)text;
    m_Size = strlen(text);
    m_Position = 0;
}


int CMemCharStream::GetCh()
{
    return (m_Position < m_Size) ? m_pStream[m_Position++] : EOF;
}


void CMemCharStream::PushBack(int ch)
{
    // hopp!

    if (m_Position > 0)
        //m_pStream[--m_Position] = ch;         // ha const char* -ot olvas ("hello world"), akkor nem lehet irni...
        --m_Position;
}


void CMemCharStream::Close()
{    
    m_Size = 0;
    m_Position = 0;
    m_pStream = 0;
}


void CMemCharStream::Seek(long pos)
{
    m_Position = (pos < (long)m_Size) ? pos : (long)m_Size;
}
